class GpermissionController {
    
    static roleAccess =  [
            [action:"*", roles:["admin"]],
        ]

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ gpermissionList: Gpermission.list( params ) ]
    }

    def show = {
        def gpermission = Gpermission.get( params.id )

        if(!gpermission) {
            flash.message = "Gpermission not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ gpermission : gpermission ] }
    }

    def delete = {
        def gpermission = Gpermission.get( params.id )
        if(gpermission) {
            gpermission.delete()
            flash.message = "Gpermission ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Gpermission not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def gpermission = Gpermission.get( params.id )

        if(!gpermission) {
            flash.message = "Gpermission not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ gpermission : gpermission ]
        }
    }

    def update = {
        def gpermission = Gpermission.get( params.id )
        if(gpermission) {
            gpermission.properties = params

		params.findAll{pm-> 
			pm.key.length()>2 && pm.key[0..2]=="otm"
		}.each {  p->
			def keys = p.key.split("_");
			def classkey = keys[1]
			def addtokeyorig = keys[2]
			def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
			classkey = classkey[0].toUpperCase() + classkey[1..-1]
		
			gpermission."$addtokeyorig"= null

			p.value.each { val->
				def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
				temp = temp.get(val)
				gpermission."$addtokey"(temp)			
			} 
		}


            if(!gpermission.hasErrors() && gpermission.save()) {
                flash.message = "Gpermission ${params.id} updated"
                redirect(action:show,id:gpermission.id)
            }
            else {
                render(view:'edit',model:[gpermission:gpermission])
            }
        }
        else {
            flash.message = "Gpermission not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def gpermission = new Gpermission()
        gpermission.properties = params
        return ['gpermission':gpermission]
    }

    def save = {
        def gpermission = new Gpermission(params)


	params.findAll{pm-> 
		pm.key.length()>2 && pm.key[0..2]=="otm"
	}.each {  p->
		def keys = p.key.split("_");
		def classkey = keys[1]
		def addtokeyorig = keys[2]
		def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
		classkey = classkey[0].toUpperCase() + classkey[1..-1]
	
		Gpermission."$addtokeyorig"= null

		p.value.each { val->
			def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
			temp = temp.get(val)
			Gpermission."$addtokey"(temp)			
		} 
	}


        if(!gpermission.hasErrors() && gpermission.save()) {
            flash.message = "Gpermission ${gpermission.id} created"
            redirect(action:show,id:gpermission.id)
        }
        else {
            render(view:'create',model:[gpermission:gpermission])
        }
    }
}
